const { Controller } = require('egg')
const { mysql } = require('../../config/plugin')
const Response = require('../utils/Response')
function isUndefined(ctx, arr) {
  let isUndefined = false
  let arr1 = [...arr]
  let str = "缺少必填字段"
  let number = 0
  arr1.forEach((item, index) => {

    if (Object.values(item)[0] == undefined || Object.values(item)[0] === "") {
      number++;
      str = str + Object.keys(item)[0] + "、"
    }
  })
  if (number > 0) {
    ctx.body = new Response(400, str, "")
    isUndefined = true
  }
  return isUndefined
}
/**
 * @Controller  公告咨询模块
 */
class trendsController extends Controller {
  // 发布发布公告、动态
  /**
* @Router post /trends/send
* @Summary 发布实验动态
* @Description 标题 内容 作者必填
* @Request body send
* @apikey
*/
  async sendTrends() {
    let { ctx, service } = this
    let { title, authorId, content, coverPhoto,trendsId} = ctx.request.body
    let isUn = isUndefined(ctx, [{ title }, { authorId }, { content }])
    if (!isUn) {
      let res = await service.trends.sendTrends(title, authorId, content, coverPhoto,trendsId)
      if (res == "发布成功"|| res=="更新成功") {
        ctx.body = new Response(200, res, "")

      } else {
        ctx.body = new Response(403, res, "")

      }
    }
  }
    // 发布发布公告、动态
  /**
* @Router post /trends/readsAdd
* @Summary 增加阅览量
* @Description trendsId为必填先
* @Request body readsAdd
* @apikey
*/
  async readsAdd(){
       let {ctx, service} = this
       let {trendsId} = ctx.request.body

       if(!trendsId) {
              ctx.body = new Response(400,"缺少必填字段trendsId","")
              return
       }else{
              let result = await service.trends.readsAdd(trendsId)
              if(result=="更新成功"){
                     ctx.body = new Response(200,result,"")
              }else{
                     ctx.body = new Response(403,result,"")
              }
       }
  }
  // 删除公告、动态
  /**
* @Router delete /trends/delete
* @Summary 删除公告或动态
* @Description trendsId为必填项
* @Request query number *trendsId
* @apikey
*/
  async deleteTrends() {
    let { ctx, service } = this
    let { trendsId } = ctx.request.body
    if (!trendsId) {
      ctx.body = new Response(400, "缺少必填字段trendsId", "")
    } else {
      let result = await service.trends.deleteTrends(trendsId)
      if (result == "删除成功") {
        ctx.body = new Response(200, result, "")
      } else {
        ctx.body = new Response(403, result, "")
      }
    }
  }
  // 根据id获取公告、动态详情
  /**
* @Router get /trends/detail
* @Summary 获取公告、动态详情
* @Description trendsId为必填项
* @Request query number *trendsId
* @apikey
*/
  async trendsDetail() {
    let { ctx, service } = this
    let { trendsId } = ctx.query
    if (!trendsId) {
      ctx.body = new Response(400, "缺少必填字段trendsId", "")
    } else {
      let result = await service.trends.trendsDetail(trendsId)
      if (result.message == "获取成功") {
        ctx.body = new Response(200, result, "")
      } else {
        ctx.body = new Response(403, result, "")
      }
    }
  }

  // 获取公告、动态列表  条件获取 title 和type
  /**
* @Router get /trends/list
* @Summary 条件获取公告、动态详情列表
* @Description 可查询条件 title 标题模糊查询 type类型查询
* @Request query string title
* @Request query string dateTime
* @Request query number type
* @apikey
*/
  async trendsList() {
    let { ctx, service } = this
    let { title, type,dateTime } = ctx.query
    let res = await service.trends.trendsList(title, type,dateTime)
    ctx.body = new Response(200, res, "")
  }



}


module.exports = trendsController